<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="阶段名称" prop="stageName">
        <el-select v-model="queryParams.stageName" style="width: 180px" placeholder="请选择" @keyup.enter.native="handleQuery">
          <el-option label="阶段一" :value="1"></el-option>
          <el-option label="阶段二" :value="2"></el-option>
          <el-option label="阶段三" :value="3"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['teaching:stage_task:add']"
        >发布任务</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['teaching:stage_task:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['teaching:stage_task:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['teaching:stage_task:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stage_taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="阶段任务id" align="center" prop="id" />-->
      <el-table-column label="阶段名称" align="center" prop="stageName" >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.stageName === 1">阶段一</el-tag>
          <el-tag type="primary" v-else-if="scope.row.stageName === 2">阶段二</el-tag>
          <el-tag type="danger" v-else-if="scope.row.stageName === 3">阶段三</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="阶段任务名称" align="center" prop="stageTaskName" />
      <el-table-column label="阶段任务描述" align="center" prop="stageTaskDescribe" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['teaching:stage_task:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSelect(scope.row)"
            v-hasPermi="['teaching:stage_task:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['teaching:stage_task:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

<!--    发布任务对话框-->
    <el-dialog title="发布任务" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="stages" :rules="rules" label-width="80px">
        <el-form-item label="阶段名称" prop="stageName">
          <el-select v-model="stages.stageName" style="width: 180px" placeholder="请选择" >
            <el-option label="阶段一" :value="1"></el-option>
            <el-option label="阶段二" :value="2"></el-option>
            <el-option label="阶段三" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="阶段任务名称" prop="stageTaskName" label-width="15%">
          <el-input v-model="stages.stageTaskName" placeholder="请输入阶段任务名称" />
        </el-form-item>
        <el-form-item label="阶段任务描述" prop="stageTaskDescribe" label-width="15%">
          <el-input v-model="stages.stageTaskDescribe" placeholder="请输入阶段任务描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改阶段任务对话框 -->


    <el-dialog title="详情" :visible.sync="xopen" width="70%" append-to-body>
      <el-form :model="classeParam" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="班级名称" prop="classId">
          <el-input
            v-model="classeParam.classId"
            placeholder="请输入班级名称"
            clearable
            @keyup.enter.native="handleQuery1"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery1">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery1">重置</el-button>
        </el-form-item>
      </el-form>


      <el-table v-loading="loading" :data="taskList" >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="班级名称" align="center" prop="classId" />
        <el-table-column label="任务描述" align="center" prop="taskDescribe" />
        <el-table-column label="阶段任务名称" align="center" prop="stageTaskName" />
        <el-table-column label="图片地址" align="center" prop="imageAddress" >
          <template slot-scope="scope">
            <image-preview :src="scope.row.imageAddress" width="40" height="30"/>
          </template>
        </el-table-column>
        <el-table-column label="学生名称" align="center" prop="studentId" >

        </el-table-column>
        <el-table-column label="任务状态" align="center" prop="status" >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 0" type="primary">待审核</el-tag>
            <el-tag v-else-if="scope.row.status === 1" type="danger">审核不通过</el-tag>
            <el-tag v-else-if="scope.row.status === 2" type="success">审核通过</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdatetasksuccess(scope.row)"
              v-hasPermi="['teaching:task:edit']"
            >批改成功</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdatetaskdefail(scope.row)"
              v-hasPermi="['teaching:task:edit']"
            >批改失败</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="Taskcancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStage_task, getStage_task, delStage_task, addStage_task, updateStage_task,getStage_task1 } from "@/api/teaching/stage_task";
import { updateTaskById1,updateTaskById2 } from "@/api/teaching/task";
export default {
  name: "Stage_task",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 阶段任务表格数据
      stage_taskList: [],
      // 任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      xopen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stageName: null,
        stageTaskName: null,
        stageTaskDescribe: null
      },
      // 班级查询参数
      classeParam:{
        className:undefined,
        stageId:undefined
      },
      // 表单参数
      form: {},
      stages:{},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();

  },
  methods: {
    /** 查询阶段任务列表 */
    getList() {
      this.loading = true;
      listStage_task(this.queryParams).then(response => {
        this.stage_taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stageName: null,
        stageTaskName: null,
        stageTaskDescribe: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      // this.title = "添加阶段任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStage_task(id).then(response => {
        this.stages = response.data;
        this.open = true;
      });
    },
    // 详情按钮操作
    handleSelect(row){
      this.reset();
      const stageId = row.id
      this.classeParam.stageId = stageId
      this.getTaskList();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.stages.id != null){
            updateStage_task(this.stages).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }else {
            addStage_task(this.stages).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }


          }

      });
    },

    /** 查询任务列表 */
    getTaskList() {
      getStage_task1(this.classeParam).then(response => {
        console.log(response)
        this.taskList = response.rows;
        this.xopen = true;
      });
    },
    // 取消按钮
    Taskcancel() {
      this.xopen = false;
      this.reset();
    },
    // 表单重置
    Taskreset() {
      this.form = {
        id: null,
        classId: null,
        taskDescribe: null,
        stageTaskName: null,
        imageAddress: null,
        studentId: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery1() {
      this.queryParams.pageNum = 1;
      this.getTaskList();
    },
    /** 重置按钮操作 */
    resetQuery1() {
      this.resetForm("queryForm");
      this.handleQuery1();
    },
    // 添加操作
    submitdetailsForm(){

    },
    /** 批改成功按钮操作 */
    handleUpdatetasksuccess(row) {
      this.reset();
      const id = row.id
      updateTaskById2({ id }).then(response => {
        this.xopen = true;
        this.$modal.msgSuccess("批改成功");
        this.getTaskList();
      });
    },
    /** 批改失败按钮操作 */
    handleUpdatetaskdefail(row) {
      this.reset();
      const id = row.id
      updateTaskById1({ id }).then(response => {
        this.xopen = true;
        this.$modal.msgSuccess("批改成功");
        this.getTaskList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除阶段任务编号为"' + ids + '"的数据项？').then(function() {
        return delStage_task(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('teaching/stage_task/export', {
        ...this.queryParams
      }, `stage_task_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
